﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmWGSQManage : Form
    {
        int iWorkMode = 0;
        SqlTools.SqlTools sqt;
        string strUserID;

        #region SQL脚本
        //获取当天最大的计划单号
        string strGetMaxPPNo = "select max(申请单号) 最大单号 from wgsqa where 申请单号 like convert(varchar(100),getdate(),112)+'%'";
        //获取服务器日期
        string strGetServerDate = "select replace(CONVERT(varchar(12) , getdate(), 111),'/','-')";
        //按条件查询指定计划单单身
        string strSelectWGSQARange = "select * from wgsqa where 申请单号 in (select distinct b.申请单号 from wgsqa a,wgsqb b where a.申请单号=b.申请单号";
        //查询指定计划单单身
        string strSelectWGSQB = "select 申请项次,合同号,型号规格,申请数量,总金额,已审核,备注 from wgsqb where 申请单号='@申请单号' order by 申请项次";
        //获取指定计划项次关联的生产计划项次
        string strGetSCJHXC = "select 生产计划号,生产计划项次,生产计划数量 from wgsqht where 申请单号='@申请单号' and 申请项次=@申请项次";
        //删除指定单号的单头
        string strDeleteWGSQA = "delete from wgsqa where 申请单号='@申请单号'";
        //删除指定单号的单身
        string strDeleteWGSQB = "delete from wgsqb where 申请单号='@申请单号'";
        //删除指定单号的合同绑定
        string strDeleteWGSQHT = "delete from wgsqht where 申请单号='@申请单号'";
        //插入单头
        string strInsertWGSQA = "insert into wgsqa (申请单号,提交人) values ('@申请单号','@提交人')";
        //插入单身
        string strInsertWGSQB = "insert into wgsqb (申请单号,申请项次,合同号,型号规格,申请数量,总金额,备注,已审核) values ('@申请单号',@申请项次,'@合同号','@型号规格',@申请数量,@总金额,'@备注',0)";
        //插入合同关联
        string strInsertWGSQHT = "insert into wgsqht (申请单号,申请项次,生产计划号,生产计划项次,生产计划数量) values ('@申请单号',@申请项次,'@生产计划号','@生产计划项次',@生产计划数量)";
        //获取单据状态
        string strSelectWGSQMark = "select 单据状态 from wgsqa where 申请单号='@申请单号'";
        //更新单据状态
        string strUpdateWGSQMark = "update wgsqa set 单据状态=@单据状态 where 申请单号='@申请单号'";
        //更新提交时间
        string strUpdateWGSQSubmitDate = "update wgsqa set 提交时间=@提交时间 where 申请单号='@申请单号'";
        #endregion

        public frmWGSQManage(string strTmpUserID)
        {
            InitializeComponent();
            strUserID = strTmpUserID;
        }

        #region 通用事件
        private void frmWGSQManage_Load(object sender, EventArgs e)
        {
            cmbDocStatus.SelectedIndex = 0;
            ButtonMode(0);
            sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
        }

        private void btnAddKeywords_Click(object sender, EventArgs e)
        {
            if (cmbSearchFields.Text.Length > 0 && txtSearchKeywords.Text.Trim().Length > 0)
            {
                if (lstKeywords.Items.Count > 0)
                {
                    for (int i = 0; i < lstKeywords.Items.Count; i++)
                    {
                        if (lstKeywords.Items[i].ToString().StartsWith(cmbSearchFields.Text))
                        {
                            lstKeywords.Items[i] = cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim();
                            return;
                        }
                    }
                }
                lstKeywords.Items.Add(cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim());
            }
        }

        private void lstKeywords_DoubleClick(object sender, EventArgs e)
        {
            if (lstKeywords.SelectedItem != null)
            {
                lstKeywords.Items.Remove(lstKeywords.SelectedItem);
            }
        }

        private void ButtonMode(int iTmpMode)
        {
            switch (iTmpMode)
            {
                case 0:      //初始状态
                    gbSearch.Enabled = true;
                    btnAdd.Enabled = true;
                    btnEdit.Enabled = false ;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = false;
                    btnCancel.Enabled = false;
                    btnGetData.Enabled = false;
                    //btnSubmit.Enabled = false;
                    break;
                case 1:      //添加状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = true;
                    btnCancel.Enabled = true;
                    btnGetData.Enabled = true;
                    //btnSubmit.Enabled = false;
                    break;
                case 2:      //编辑状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = true;
                    btnCancel.Enabled = true;
                    btnGetData.Enabled = true;
                    //btnSubmit.Enabled = false;
                    break;
                default:
                    clsTools.ShowWarningMsg(iTmpMode.ToString() + ":未设置的工作状态！");
                    break;
            }
        }

        private void FillRowIndex(DataGridView tmpdgv,string strTmpIDFieldName)
        {
            if (tmpdgv.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdgv.Rows.Count; i++)
                {
                    tmpdgv.Rows[i].Cells[strTmpIDFieldName].Value = i+1;
                }
            }
        }
        #endregion 通用事件

        private string GetPPNo()
        {
            string strTmpMaxPPNo = clsTools.GetString(sqt.DoCmdScalarData(strGetMaxPPNo));
            string strTmpDate = string.Empty;
            string strTmpNo = string.Empty;
            if (strTmpMaxPPNo.Trim().Length == 0)
            {
                strTmpDate = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate)).Replace("-", "");
                strTmpNo = "0001";
            }
            else
            {
                strTmpDate = strTmpMaxPPNo.Substring(0, 8);
                int iTmpNo = clsTools.GetInt(strTmpMaxPPNo.Substring(8, 4)) + 1;
                strTmpNo = iTmpNo.ToString("0000");
            }
            return strTmpDate + strTmpNo;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (clsTools.VerifyPermission(strUserID, "sq3"))
            {
                iWorkMode = 1;                                                                //设置工作模式为新增模式
                if (dgv1.DataSource != null)
                {
                    DataTable tmpdt = (DataTable)dgv1.DataSource;
                    tmpdt.Rows.Add(tmpdt.NewRow());
                }
                else
                {
                    dgv1.Rows.Add();
                }
                dgv1.Rows[dgv1.Rows.Count - 1].Cells["dgv1_c1"].Value = GetPPNo();
                dgv1.Rows[dgv1.Rows.Count - 1].Cells["dgv1_c2"].Value = strUserID;
                dgv1.CurrentCell = dgv1.Rows[dgv1.Rows.Count - 1].Cells[0];
                dgv1.Enabled = false;
                lblMark.Text = "";
                lblMark.Visible = false;
                dgv2.ContextMenuStrip = contextMenuStrip1;
                dgv2.Rows.Clear();
                dgv2.ReadOnly = false;
                ButtonMode(1);
            }
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            frmPPSelector newFrmPPSelector = new frmPPSelector();
            newFrmPPSelector.Owner = this.Owner;
            newFrmPPSelector.ShowDialog();
            if (newFrmPPSelector.bCancel || newFrmPPSelector.dfrlst == null)
            {
                return;
            }
            if (newFrmPPSelector.dfrlst.Count > 0)
            {
                dgv2.Rows.Add(newFrmPPSelector.dfrlst.Count);
                for (int i = 0; i < newFrmPPSelector.dfrlst.Count; i++)
                {
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c2"].Value = newFrmPPSelector.dfrlst[i].strModel;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c3"].Value = newFrmPPSelector.dfrlst[i].iSum;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c5"].Value = newFrmPPSelector.dfrlst[i].strMemo;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c6"].Value = newFrmPPSelector.dfrlst[i].strPPNo;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c7"].Value = newFrmPPSelector.dfrlst[i].strPX;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c8"].Value = newFrmPPSelector.dfrlst[i].iQuantity;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c9"].Tag  = newFrmPPSelector.dfrlst[i].strContract;
                    if (newFrmPPSelector.dfrlst[i].strContract.Length > 1)
                    {
                        dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c9"].Value = "多项";
                    }
                    else
                    {
                        dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c9"].Value = newFrmPPSelector.dfrlst[i].strContract[0];
                    }
                }
                dgv2.AutoResizeColumns();
                FillRowIndex(dgv2,"dgv2_c1");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (clsTools.VerifyPermission(strUserID, "sq2"))
            {
                string strTmpSelectString = strSelectWGSQARange;
                for (int i = 0; i < lstKeywords.Items.Count; i++)
                {
                    string strTmpFieldName = lstKeywords.Items[i].ToString().Split(':').GetValue(0).ToString();
                    string strTmpKeyword = lstKeywords.Items[i].ToString().Split(':').GetValue(1).ToString();
                    switch (strTmpFieldName)
                    {
                        case "申请单号":
                            strTmpSelectString += " and b.申请单号 like '" + strTmpKeyword + "'";
                            break;
                        case "合同号":
                            strTmpSelectString += " and ((select count(*) from scjhb_table,wgsqht where jhb001=生产计划号 and jhb002=生产计划项次 and wgsqht.申请单号=b.申请单号 and jhb024 like '" + strTmpKeyword + "')>0 or 合同号 like '"+strTmpKeyword +"')";
                            break;
                        case "型号规格":
                            strTmpSelectString += " and 型号规格 like '" + strTmpKeyword + "'";
                            break;
                        case "提交人":
                            strTmpSelectString += " and 提交人 like '" + strTmpSelectString + "'";
                            break;
                    }
                }
                switch (cmbDocStatus.SelectedItem.ToString())
                {
                    case "未提交":
                        strTmpSelectString += " and 单据状态=0";
                        break;
                    case "已提交":
                        strTmpSelectString += " and 单据状态=1";
                        break;
                    case "未审核完":
                        strTmpSelectString += " and 单据状态=1 and (select count(*) from wgsqb where 申请单号=wgsqa.申请单号 and 已审核=0)>0";
                        break;
                    case "已审核完":
                        strTmpSelectString += " and 单据状态=1 and (select count(*) from wgsqb where 申请单号=wgsqa.申请单号 and 已审核=0)=0";
                        break;
                }
                strTmpSelectString += ")";
                dgv1.DataSource = sqt.GetDataTable(strTmpSelectString);
                if (dgv1.Rows.Count > 0)
                {
                    dgv1.CurrentCell = dgv1.Rows[0].Cells[0];
                }
            }
        }

        private void ReadData()
        {
            ButtonMode(0);
            if (dgv1.CurrentRow != null)
            {
                string strTmpSqNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                DataTable tmpdt = sqt.GetDataTable(strSelectWGSQB.Replace("@申请单号", strTmpSqNo));
                switch (clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c4"].Value).Trim())
                {
                    case "0":
                        lblMark.Text = "";
                        lblMark.Visible = false;
                        btnDelete.Enabled = true;
                        btnSubmit.Enabled = true;
                        btnEdit.Enabled = true;
                        break;
                    case "1":
                        lblMark.Text = "提";
                        lblMark.Visible = true;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = true;
                        btnEdit.Enabled = false;
                        break;
                }
                dgv2.Rows.Clear();
                if (tmpdt.Rows.Count > 0)
                {
                    dgv2.Rows.Add(tmpdt.Rows.Count);
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        dgv2.Rows[i].Cells["dgv2_c1"].Value = tmpdt.Rows[i]["申请项次"];
                        dgv2.Rows[i].Cells["dgv2_c2"].Value = tmpdt.Rows[i]["型号规格"];
                        dgv2.Rows[i].Cells["dgv2_c3"].Value = tmpdt.Rows[i]["申请数量"];
                        dgv2.Rows[i].Cells["dgv2_c4"].Value = tmpdt.Rows[i]["已审核"];
                        dgv2.Rows[i].Cells["dgv2_c5"].Value = tmpdt.Rows[i]["备注"];
                        dgv2.Rows[i].Cells["dgv2_c9"].Value = tmpdt.Rows[i]["合同号"];
                        dgv2.Rows[i].Cells["dgv2_c10"].Value = tmpdt.Rows[i]["总金额"];
                        if (clsTools.GetInt(dgv2.Rows[i].Cells["dgv2_c4"].Value) > 0)
                        {
                            btnSubmit.Enabled = false;
                        }
                        string[] strTmpPP = new string[1];
                        string[] strTmpPX = new string[1];
                        int[] iTmpQuantity = new int[1];
                        DataTable tmpdtscjh = sqt.GetDataTable(strGetSCJHXC.Replace("@申请单号", strTmpSqNo)
                                                                                                    .Replace("@申请项次", tmpdt.Rows[i]["申请项次"].ToString()));
                        if (tmpdtscjh.Rows.Count > 0)
                        {
                            strTmpPP = new string[tmpdtscjh.Rows.Count];
                            strTmpPX = new string[tmpdtscjh.Rows.Count];
                            iTmpQuantity = new int[tmpdtscjh.Rows.Count];
                            for (int k = 0; k < tmpdtscjh.Rows.Count; k++)
                            {
                                strTmpPP[k] = tmpdtscjh.Rows[k]["生产计划号"].ToString().Trim();
                                strTmpPX[k] = tmpdtscjh.Rows[k]["生产计划项次"].ToString().Trim();
                                iTmpQuantity[k] = clsTools.GetInt(tmpdtscjh.Rows[k]["生产计划数量"].ToString());
                            }
                        }
                        dgv2.Rows[i].Cells["dgv2_c6"].Value = strTmpPP;
                        dgv2.Rows[i].Cells["dgv2_c7"].Value = strTmpPX;
                        dgv2.Rows[i].Cells["dgv2_c8"].Value = iTmpQuantity;
                    }
                    dgv2.AutoResizeColumns();
                }
            }
            else
            {
                dgv2.Rows.Clear();
            }
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            ReadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (clsTools.VerifyPermission(strUserID, "sq4"))
            {
                iWorkMode = 2;                                                                //设置工作模式为编辑模式
                dgv1.Enabled = false;
                dgv2.ContextMenuStrip = contextMenuStrip1;
                dgv2.ReadOnly = false;
                ButtonMode(2);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (clsTools.VerifyPermission(strUserID, "sq5"))
            {
                if (dgv1.CurrentRow != null)
                {
                    string strTmpSqNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value).Trim();
                    DialogResult dr = MessageBox.Show("确定删除单号为" + strTmpSqNo + "的申请单？删除以后将无法恢复！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dr == DialogResult.OK)
                    {
                        sqt.DoCmdExecuteNonQuery(strDeleteWGSQA.Replace("@申请单号", strTmpSqNo));
                        sqt.DoCmdExecuteNonQuery(strDeleteWGSQB.Replace("@申请单号", strTmpSqNo));
                        sqt.DoCmdExecuteNonQuery(strDeleteWGSQHT.Replace("@申请单号", strTmpSqNo));
                        dgv1.Rows.Remove(dgv1.CurrentRow);
                    }
                }
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex(dgv2,"dgv2_c1");
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c9"].Value).Trim().Length == 0 || clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value).Trim().Length == 0 || clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value).Trim().Length == 0)
                                {
                                    clsTools.ShowWarningMsg("合同号、型号规格和申请数量不允许为空！");
                                    return;
                                }
                            }
                            string strTmpSqNo = GetPPNo();
                            dgv1.CurrentRow.Cells["dgv1_c1"].Value = strTmpSqNo;
                            dgv1.CurrentRow.Cells["dgv1_c2"].Value = strUserID ;
                            if (sqt.DoCmdExecuteNonQuery(strInsertWGSQA.Replace("@申请单号", strTmpSqNo )
                                                                                                        .Replace ("@提交人",strUserID)) > 0)
                            {
                                for (int i = 0; i < dgv2.Rows.Count; i++)
                                {
                                    if (dgv2.Rows[i].Cells["dgv2_c6"].Value != null && dgv2.Rows[i].Cells["dgv2_c7"].Value != null && dgv2.Rows[i].Cells["dgv2_c8"].Value != null)
                                    {
                                        string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c6"].Value;
                                        string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c7"].Value;
                                        int[] iTmpQuantity = (int[])dgv2.Rows[i].Cells["dgv2_c8"].Value;
                                        for (int k = 0; k < strTmpPP.Length; k++)
                                        {
                                            if (strTmpPP[k].Trim().Length == 0)
                                            {
                                                continue;
                                            }
                                            sqt.DoCmdExecuteNonQuery(strInsertWGSQHT.Replace("@申请单号", strTmpSqNo )
                                                                                                                             .Replace("@申请项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                             .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                             .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                             .Replace("@生产计划数量", iTmpQuantity[k].ToString()));
                                        }
                                    }
                                    sqt.DoCmdExecuteNonQuery(strInsertWGSQB.Replace("@申请单号", strTmpSqNo)
                                                                                                                         .Replace("@申请项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                        .Replace("@申请数量", clsTools.GetInt(dgv2.Rows[i].Cells["dgv2_c3"].Value).ToString ())
                                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                                        .Replace ("@合同号",clsTools .GetString (dgv2.Rows[i].Cells["dgv2_c9"].Value))
                                                                                                                        .Replace ("@总金额",clsTools.GetDouble (dgv2.Rows[i].Cells["dgv2_c10"].Value).ToString ("0.00")));
                                }
                            }
                            ButtonMode(0);
                            btnEdit.Enabled = true;
                            btnDelete.Enabled = true;
                            btnConfirm.Enabled = false;
                            btnSubmit.Enabled = true;
                            dgv2.ReadOnly = true;
                            dgv2.ContextMenuStrip = null;
                            dgv1.Enabled =true ;
                            iWorkMode = 0;
                        }
                    }
                    break;
                case 2:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex(dgv2,"dgv2_c1");
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value).Trim().Length == 0 || clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value).Trim().Length == 0)
                                {
                                    clsTools.ShowWarningMsg("型号规格和申请数量不允许为空！");
                                    return;
                                }
                            }
                            string strTmpSqNo=clsTools .GetString ( dgv1.CurrentRow .Cells ["dgv1_c1"].Value ).Trim ();
                            sqt.DoCmdExecuteNonQuery(strDeleteWGSQB.Replace("@申请单号", strTmpSqNo));
                            sqt.DoCmdExecuteNonQuery(strDeleteWGSQHT.Replace("@申请单号", strTmpSqNo));
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (dgv2.Rows[i].Cells["dgv2_c6"].Value != null && dgv2.Rows[i].Cells["dgv2_c7"].Value != null && dgv2.Rows[i].Cells["dgv2_c8"].Value != null)
                                {
                                    string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c6"].Value;
                                    string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c7"].Value;
                                    int[] iTmpQuantity = (int[])dgv2.Rows[i].Cells["dgv2_c8"].Value;
                                    for (int k = 0; k < strTmpPP.Length; k++)
                                    {
                                        if (strTmpPP[k].Trim().Length == 0)
                                        {
                                            continue;
                                        }
                                        sqt.DoCmdExecuteNonQuery(strInsertWGSQHT.Replace("@申请单号", strTmpSqNo)
                                                                                                                             .Replace("@申请项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                             .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                             .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                             .Replace("@生产计划数量", iTmpQuantity[k].ToString()));
                                    }
                                }
                                sqt.DoCmdExecuteNonQuery(strInsertWGSQB.Replace("@申请单号", strTmpSqNo)
                                                                                                                         .Replace("@申请项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                        .Replace("@申请数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                                        .Replace("@合同号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c9"].Value))
                                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                                        .Replace("@总金额", clsTools.GetDouble(dgv2.Rows[i].Cells["dgv2_c10"].Value).ToString("0.00")));
                            }
                            ButtonMode(0);
                            btnEdit.Enabled = true;
                            btnDelete.Enabled = true;
                            btnConfirm.Enabled = false ;
                            btnSubmit.Enabled = true;
                            dgv2.ReadOnly = true;
                            dgv2.ContextMenuStrip = null;
                            dgv1.Enabled =true ;
                            iWorkMode = 0;
                        }
                    }
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:       //添加状态
                    iWorkMode = 0;
                    ButtonMode(0);
                    dgv1.Rows.Remove(dgv1.CurrentRow);
                    dgv2.Rows.Clear();
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
                case 2:       //编辑状态
                    iWorkMode = 0;
                    ButtonMode(0);
                    btnDelete.Enabled = true;
                    btnEdit.Enabled = true;
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
            }
        }

        private void 添加行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgv2.Rows.Add();
            dgv2.CurrentCell = dgv2.Rows[dgv2.Rows.Count - 1].Cells[0];
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv2.CurrentRow != null)
            {
                dgv2.Rows.Remove(dgv2.CurrentRow);
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (clsTools.VerifyPermission(strUserID, "sq6"))
            {
                if (dgv1.CurrentRow != null)
                {
                    string strTmpSqNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                    int iMark = clsTools.GetInt(sqt.DoCmdScalarData(strSelectWGSQMark.Replace("@申请单号", strTmpSqNo)));
                    if (iMark == 0)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateWGSQMark.Replace("@申请单号", strTmpSqNo)
                                                                                                       .Replace("@单据状态", "1"));
                        sqt.DoCmdExecuteNonQuery(strUpdateWGSQSubmitDate.Replace("@申请单号", strTmpSqNo)
                                                                                                                 .Replace("@提交时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                        dgv1.CurrentRow.Cells["dgv1_c3"].Value = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate));
                        dgv1.CurrentRow.Cells["dgv1_c4"].Value = 1;
                        lblMark.Text = "提";
                        lblMark.Visible = true;
                        btnEdit.Enabled = false;
                        btnDelete.Enabled = false;
                        btnCancel.Enabled = false;
                        btnConfirm.Enabled = false;
                    }
                    else
                    {
                        if (iMark == 1)
                        {
                            sqt.DoCmdExecuteNonQuery(strUpdateWGSQMark.Replace("@申请单号", strTmpSqNo)
                                                                                                                        .Replace("@单据状态", "0"));
                            sqt.DoCmdExecuteNonQuery(strUpdateWGSQSubmitDate.Replace("@申请单号", strTmpSqNo)
                                                                                                                                        .Replace("@提交时间", "null"));
                            dgv1.CurrentRow.Cells["dgv1_c3"].Value = string.Empty;
                            dgv1.CurrentRow.Cells["dgv1_c4"].Value = 0;
                            lblMark.Text = "";
                            lblMark.Visible = false;
                            btnEdit.Enabled = true;
                            btnDelete.Enabled = true;
                            btnCancel.Enabled = false;
                            btnConfirm.Enabled = false;
                        }
                    }
                }
            }
        }

        private void dgv2_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv2.CurrentCell != null && dgv2.Columns[dgv2.CurrentCell.ColumnIndex].Name == "dgv2_c2" && dgv2.ReadOnly == false)
            {
                btnBrowseCPBM.Height = dgv2.CurrentCell.Size.Height;
                btnBrowseCPBM.Left = dgv2 .GetCellDisplayRectangle (dgv2 .CurrentCell .ColumnIndex ,dgv2 .CurrentCell .RowIndex ,true ).X + dgv2.Left  + dgv2.CurrentCell.Size.Width - btnBrowseCPBM.Width;
                btnBrowseCPBM.Top = dgv2.GetCellDisplayRectangle(dgv2.CurrentCell.ColumnIndex, dgv2.CurrentCell.RowIndex, true).Y+dgv2 .Top ;
                btnBrowseCPBM.Visible = true;
            }
            else
            {
                btnBrowseCPBM.Visible = false;
            }
        }

        private void btnBrowseCPBM_Click(object sender, EventArgs e)
        {
            dgv2 .CurrentCell .Value = frmCPBMSelector.ShowForm(this);
            dgv2.AutoResizeColumn(2);
        }

        private void dgv2_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            if (e.Column.Name == "dgv2_c2"&&btnBrowseCPBM .Visible ==true)
            {
                btnBrowseCPBM.Height = dgv2.CurrentCell.Size.Height;
                btnBrowseCPBM.Left = dgv2.GetCellDisplayRectangle(dgv2.CurrentCell.ColumnIndex, dgv2.CurrentCell.RowIndex, true).X + dgv2.Left + dgv2.CurrentCell.Size.Width - btnBrowseCPBM.Width;
                btnBrowseCPBM.Top = dgv2.GetCellDisplayRectangle(dgv2.CurrentCell.ColumnIndex, dgv2.CurrentCell.RowIndex, true).Y + dgv2.Top;
            }
        }

        private void dgv2_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                btnBrowseCPBM.Visible = false;
            }
        }

        private void dgv2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                btnBrowseCPBM.Visible = true;
            }
            if (e.ColumnIndex == 3)
            {
                if (clsTools.GetString(dgv2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Length > 0)
                {
                    dgv2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = clsTools.GetInt(dgv2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                }
            }
            if (e.ColumnIndex == 4)
            {
                if (clsTools.GetString(dgv2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Length > 0)
                {
                    dgv2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = clsTools.GetDouble (dgv2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).ToString ("0.00");
                }
            }
        }

        private void 标注预排ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv2.SelectedCells.Count > 0)
            {
                for (int i = 0; i < dgv2.SelectedCells.Count; i++)
                {
                    dgv2.Rows[dgv2.SelectedCells[i].RowIndex].Cells["dgv2_c9"].Value = "预排";
                }
            }
        }

        private void dgv2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1&&dgv2.CurrentCell !=null &&dgv2 .CurrentCell .Tag !=null )
            {
                frmDetailViewer newFrmDetailViewer = new frmDetailViewer((string[])dgv2.CurrentCell.Tag);
                newFrmDetailViewer.ShowDialog();
            }
        }
    }
}